﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RiskMan.Config;
using RiskMan.DataClass.Models.Quik;
using XlDde;
using RiskMan.DataClass.Models;
using System.Windows.Forms;
using RiskMan.Views.Dialogs;
using System.Reflection;

namespace RiskMan.Providers.Channels
{
    class DDEChannelPortfel : XlDdeChannel
    {
        private List<PortfelModel> list;

        public DDEChannelPortfel()
        {
            
        }

        protected override void ProcessTable(XlTable xt, string topic)
        {
            list = RiskMan.MainWindow.StaticRiskMan._dataProvider.Portfel;
            try
            {
                for (int i = 0; i < xt.Rows; i++)
                {
                    PortfelModel tmp = new PortfelModel();
                    List<String> names = new List<String>();

                    for (int j = 0; j < xt.Columns; j++)
                    {
                        xt.ReadValue();
                        if (Namespace.Count == 0 && i == 0)
                        {
                            tmp = null;
                            names.Add(xt.StringValue);
                        }
                        else
                        {
                            if (IsSend(xt) == true)
                            {
                                try
                                {
                                    if ( Namespace [ j ] == "FIRMID" ) tmp.Firm = xt.StringValue;
                                    if ( Namespace [ j ] == "CLIENTCODE" ) tmp.CodeClient = xt.StringValue;
                                    if ( Namespace [ j ] == "ISQUALCLIENT" ) tmp.BigestLevelRisk = xt.StringValue;
                                    if ( Namespace [ j ] == "ISMARGIN" ) tmp.TypeClient = xt.StringValue;
                                    if ( Namespace [ j ] == "ISFUTURES" ) tmp.SrochAccount = xt.StringValue;
                                    if ( Namespace [ j ] == "INASSETS" ) tmp.EnterActive = xt.FloatValue;
                                    if ( Namespace [ j ] == "LEVERAGE" ) tmp.Laverage = xt.FloatValue;
                                    if ( Namespace [ j ] == "OPENLIMIT" ) tmp.EnterLimite = xt.FloatValue;
                                    if ( Namespace [ j ] == "VALSHORT" ) tmp.Short = xt.FloatValue;
                                    if ( Namespace [ j ] == "VALLONG" ) tmp.Long = xt.FloatValue;
                                    if ( Namespace [ j ] == "VALLONGM" ) tmp.LongMO = xt.FloatValue;
                                    if ( Namespace [ j ] == "VALLONGA" ) tmp.LongO = xt.FloatValue;
                                    if ( Namespace [ j ] == "ASSETS" ) tmp.CurrentActives = xt.FloatValue;
                                    if ( Namespace [ j ] == "CURLEVERAGE" ) tmp.CurrentLaverage = xt.FloatValue;
                                    if ( Namespace [ j ] == "MARGIN" ) tmp.LevelMarga = xt.FloatValue;
                                    if ( Namespace [ j ] == "LIMALL" ) tmp.CurrentLimite = xt.FloatValue;
                                    if ( Namespace [ j ] == "AVLIMALL" ) tmp.DostCurrentLimite = xt.FloatValue;
                                    if ( Namespace [ j ] == "LOCKEDBUY" ) tmp.BlockBuy = xt.FloatValue;
                                    if ( Namespace [ j ] == "LOCKEDBUYM" ) tmp.BlockBuyMo = xt.FloatValue;
                                    if ( Namespace [ j ] == "LOCKEDBUYA" ) tmp.BlockBuyO = xt.FloatValue;
                                    if ( Namespace [ j ] == "LOCKEDSELL" ) tmp.BlockSell = xt.FloatValue;
                                    if ( Namespace [ j ] == "LOCKEDVALUECOEF" ) tmp.BlockBuyNoMarge = xt.FloatValue;
                                    if ( Namespace [ j ] == "INALLASSETS" ) tmp.EnterSredstva = xt.FloatValue;
                                    if ( Namespace [ j ] == "ALLASSETS" ) tmp.CurrentSredstva = xt.FloatValue;
                                    if ( Namespace [ j ] == "PROFITLOSS" ) tmp.ProfitLoss = xt.FloatValue;
                                    if ( Namespace [ j ] == "RATECHANGE" ) tmp.PersentChange = xt.FloatValue;
                                    if ( Namespace [ j ] == "LIMBUY" ) tmp.OnBuy = xt.FloatValue;
                                    if ( Namespace [ j ] == "LIMSELL" ) tmp.OnSell = xt.FloatValue;
                                    if ( Namespace [ j ] == "LIMNONMARGIN" ) tmp.OnBuyNoMarge = xt.FloatValue;
                                    if ( Namespace [ j ] == "LIMBUYA" ) tmp.OnBuyObesp = xt.FloatValue;
                                    if ( Namespace [ j ] == "GOFORPOSITIONS" ) tmp.GOPositions = xt.FloatValue;
                                    if ( Namespace [ j ] == "GOFORORDERS" ) tmp.GOOrders = xt.FloatValue;
                                    if ( Namespace [ j ] == "VARMARGIN" ) tmp.VariableMarg = xt.FloatValue;
                                    if ( Namespace [ j ] == "RATEFUTURES" ) tmp.ActivedOnGO = xt.FloatValue;
                                    if ( Namespace [ j ] == "TOTALMONEYBAL" ) tmp.SummMoneyOstatoc = xt.FloatValue;
                                    if ( Namespace [ j ] == "TOTALLOCKEDMONEY" ) tmp.SummBlock = xt.FloatValue;
                                    if ( Namespace [ j ] == "CURRTAG" ) tmp.ParametersRaschet = xt.StringValue;
                                }
                                catch ( ArgumentOutOfRangeException )
                                {
                                    string message = "\"Клиентский портфель\" в разных терминалах настроены по разному. \n\r" +
                                        "Для правильной настройки таблице ознакомьтесь с инструкцией." +
                                        "\n\rНеобходимо чтоб название и количество выводимых параметров совпадало.";
                                    if ( base.IsShowMessage == false )
                                    {
                                        base.IsShowMessage = true;
                                        new DialogMessage( message, "Ошибка" );
                                    }
                                    cfg.SetLog( message, RiskMan.MainWindow.StaticRiskMan.ListBox_Logs, GetType( ), MethodBase.GetCurrentMethod( ) );
                                }
                                catch ( Exception ex )
                                {
                                    new DialogMessage( ex.Message, "DDEChannelPortfel" );
                                }
                            }
                            else
                            { tmp = null;  }
                        }
                    }
                    if (names.Count > 0)
                        Namespace = new List<String>(names);
                    if (tmp != null)
                    {
                        bool tr = false;
                        for (int j = 0; j < list.Count; j++)
                            if (list[j].CodeClient == tmp.CodeClient && list[j].Firm == tmp.Firm)
                            {
                                tr = true;
                                list[j] = tmp;
                            }
                        if (tr == false)
                            list.Add(tmp);
                    }
                }
            }
            catch (Exception ex)
            {
                new DialogOkCancel( ex.Message, "DDEChannelPortfel" );
            }
        }

        public override void Disconnected(bool isConnected)
        {
            QuikIntegration.dde.QuikUtil.StartDDE();
            Namespace.Clear();
        }
    }
}
